/*
 * @author java_deng
 * @date 2024/12/4 17:20
 * @description
 */
package org.kaka.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.kaka.dto.UserDto;
import org.kaka.mapper.FileMapper;
import org.kaka.mapper.UserMapper;
import org.kaka.po.UserPo;
import org.kaka.service.UserService;
import org.kaka.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.management.ServiceNotFoundException;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private FileMapper fileMapper;

    @Override
    public UserVo login(UserDto userDto) {

        UserDto user=userMapper.findByAccount(userDto.getAccount(),userDto.getAuth());
        if(user==null){
            return null;
        }
        if(!user.getPassword().equals(userDto.getPassword()))
        {
            return null;
        }
        UserVo userVo=new UserVo();
        BeanUtils.copyProperties(user,userVo);
        userVo.setProfilePath(fileMapper.findPathById(userVo.getProfile()));
        return userVo;
    }

    @Override
    public void save(UserDto userDto) throws ServiceNotFoundException {
        if(userDto.getAuth()==null){
            userDto.setAuth(1);
        }
        userDto.setIsDeleted(0);
        try {
            userMapper.insert(userDto);
        }catch (Exception e){
            Throwable cause = e.getCause();
            if (cause instanceof SQLIntegrityConstraintViolationException){
                String errMsg =   (cause).getMessage();
                if (StringUtils.isNotEmpty(errMsg) && errMsg.contains("account")){
                    throw new ServiceNotFoundException("账号已经存在");
                }
            }
            throw new ServiceNotFoundException("参数错误");
        }

    }

    @Transactional
    @Override
    public UserVo update(UserDto userDto) {
        userMapper.updateById(userDto);
        UserVo res = userMapper.deail(userDto.getId());
        res.setProfile(userDto.getProfile());
        res.setProfilePath(fileMapper.findPathById(userDto.getProfile()));
        return res;
    }

    @Override
    public UserVo updateUserInfo(UserDto userDto) {
        userMapper.updateById(userDto);
        return userMapper.deail(userDto.getId());
    }

    public Page<UserVo> list(UserPo userPo) {
        if(userPo.getPageIndex()==null){
            userPo.setPageIndex(1);
        }
        if(userPo.getPageSize()==null){
            userPo.setPageSize(10);
        }
        if(userPo.getAuth()==null){
            userPo.setAuth(1);
        }
        Page<UserVo> objects = PageHelper.startPage(userPo.getPageIndex(), userPo.getPageSize());
        userMapper.list(userPo);
        return objects;
    }

    public UserVo detail(Long id) {
        return userMapper.detail(id);
    }

    public void delete(Long id) {
        userMapper.deleteById(id);
    }
}
